package EstimatorPackage.Expressions;

import EstimatorPackage.Expressions.Expression;
import java.util.Collection;
import java.util.HashSet;

/**
 * Class for objects representing list-expression.
 * List-expression are either the union or the intersection of a list of expression.
 *   ⋃ a1, a2 ... an = a1 ∪ a2 ∪ ... ∪ an
 *   ⋂ a1, a2 ... a3 = a1 ∩ a2 ∩ ... ∩ an
 * @author Martin Haulrich
 */
public class ListExpression implements Expression {

    public final static int UNION = 2;

    public final static int INTERSECTION = 3;

    public final static int type = Expression.LIST;

    public final int operation;

    public final Collection<Expression> expList;

    public final int hash;

    public ListExpression(int operation) {
        this.operation = operation;
        expList = new HashSet<Expression>();
        hash = -3;
    }

    public ListExpression(int operation, Collection<Expression> expList) {
        this.operation = operation;
        this.expList = expList;
        /**
         * @CONSIDER Is this a good hash code?
         */
        int h = 7;
        for (Expression e : expList) {
            h = h + (19 * e.hashCode());
        }
        hash = h;
    }

    @Override
    public int type() {
        return type;
    }

    @Override
    public String toString() {
        String op;
        switch (operation) {
            case UNION:
                //op = "\u22c3";
                op = "U";
                break;
            case INTERSECTION:
                //op = "\u22c2";
                op = "INT";
                break;
            default:
                op = " ";
        }

        return op + expList;

    }

    @Override
    public boolean equals(Object o) {

        if (o instanceof ListExpression) {
            ListExpression le = (ListExpression) o;
            return ((operation == le.operation) && (expList.equals(le.expList)));

        }
        return false;
    }

    @Override
    public int hashCode() {
        return hash;
    }
}
